﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEngine;

public class TestData
{
    public int age;
    public string name;
    public int number;
    public bool gender;

    public static void SavaData(TestData data,string fileName)
    {
        Debug.Log(Application.persistentDataPath);
        string path = Application.persistentDataPath + '/' + fileName + ".txt";

        FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);

        FieldInfo[] fieldInfos = data.GetType().GetFields();

        for (int i = 0; i < fieldInfos.Length; i++)
        {
            byte[] bytes;
            //object tobj = fieldInfos[i].GetValue(data);
            if (fieldInfos[i].FieldType == typeof(int))
            {
                bytes = BitConverter.GetBytes((int)fieldInfos[i].GetValue(data));
                fs.Write(bytes, 0, 4);
            }
            else if (fieldInfos[i].FieldType == typeof(string))
            {
                bytes = Encoding.UTF8.GetBytes((string)fieldInfos[i].GetValue(data));
                //先存字符串的字节长度
                fs.Write(BitConverter.GetBytes(bytes.Length), 0, 4);
                fs.Write(bytes, 0, bytes.Length);
            }
            else if (fieldInfos[i].FieldType == typeof(bool))
            {
                bytes = BitConverter.GetBytes((bool)fieldInfos[i].GetValue(data));
                fs.Write(bytes, 0, 1);
            }
        }

        fs.Flush();
        fs.Dispose();
    }

    public static TestData LoadData(string fileName)
    {
        string path = Application.persistentDataPath + '/' + fileName + ".txt";
        if (!File.Exists(path))
        {
            return null;
        }

        FileStream fs = File.Open(path, FileMode.Open, FileAccess.ReadWrite);

        TestData obj = Activator.CreateInstance<TestData>();

        FieldInfo[] infos = typeof(TestData).GetFields();
        for (int i = 0; i < infos.Length; i++)
        {
            byte[] bytes;
            if (infos[i].FieldType == typeof(int))
            {
                bytes = new byte[4];

                fs.Read(bytes, 0, 4);
                int num = BitConverter.ToInt32(bytes, 0);
                infos[i].SetValue(obj, num);
            }
            else if (infos[i].FieldType == typeof(string))
            {
                bytes = new byte[4];

                //获得字符串字节长度
                fs.Read(bytes, 0, 4);
                int len = BitConverter.ToInt32(bytes, 0);

                bytes = new byte[len];

                fs.Read(bytes, 0,len);
                string s = Encoding.UTF8.GetString(bytes);

                infos[i].SetValue(obj, s);
            }
            else if (infos[i].FieldType == typeof(bool))
            {
                bytes = new byte[1];

                fs.Read(bytes, 0, 1);
                bool num = BitConverter.ToBoolean(bytes, 0);
                infos[i].SetValue(obj, num);
            }
        }
        fs.Flush();
        fs.Dispose();
        return obj;
    }
}
